package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.Event.EventChooseAddress;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.AddressItem;
import com.zadtek.shopmall.utils.ToastUtil;
import com.zadtek.shopmall.view.SpacesItemDecoration;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedList;
import java.util.List;

public class ChooseAddressActivity extends AbsActivity {

    private ImageView ivBack;
    private TextView topTitle,tvAddAddress;
    private RecyclerView recyclerView;
    private List<AddressItem> data = new LinkedList<>();
    private AddressAdapter adapter;
    private int page =1;
    private boolean canLoadMore = true;
    private int from;
    @Override
    public int setView() {
        return R.layout.activity_address_list;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        from = getIntent().getIntExtra("from",-1);
        initView();
        initLogic();
        requestData();
    }

    private void initView() {
        ivBack = (ImageView) findViewById(R.id.ic_back);
        ivBack.setVisibility(View.VISIBLE);
        topTitle = (TextView) findViewById(R.id.title);
        topTitle.setText("选择地址");
        tvAddAddress = (TextView) findViewById(R.id.tv_add_address);
        recyclerView = (RecyclerView)findViewById(R.id.recycleview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new SpacesItemDecoration(getResources().getDimensionPixelSize(R.dimen.little_margin)));
    }

    private void initLogic(){
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvAddAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addNewAddress();
            }
        });
        adapter = new AddressAdapter(R.layout.address_item,data);
        recyclerView.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                requestData();
            }
        },recyclerView);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                EventBus.getDefault().post(new EventChooseAddress(data.get(position),from));
                finish();
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.tv_name:
                        break;
                    case R.id.tv_phonenumber:
                        break;
                    case R.id.tv_address:
                        break;
                    case R.id.cb_default:
                        ToastUtil.showToast("设为默认请求");
                        chooseDefault(position);
                        break;
                    case R.id.tv_edit:
                        startActivity(new Intent(ChooseAddressActivity.this,EditAddressActivity.class).putExtra("addressitem",data.get(position)));
                        ToastUtil.showToast("编辑请求");
                        break;
                    case R.id.tv_delete:
                        ToastUtil.showToast("删除请求！");
                        break;
                }
            }
        });
    }

    private void chooseDefault(int position) {
        for(AddressItem item:data){
            item.setDefault(false);
        }
        data.get(position).setDefault(true);
        adapter.notifyDataSetChanged();
    }

    private void requestData() {
        if(!canLoadMore){
            return;
        }
        AddressItem item = new AddressItem();
        data.add(item);
        item.setPhone("123232323223");
        item.setName("zackdk");
        item.setAddressDetail("sdksdksjkhfddsjkhfkjs");
        item.setAddressArae("地方还会空间的很快就");
        AddressItem item2 = new AddressItem();
        if(page == 1)
            item2.setDefault(true);
        data.add(item2);
        adapter.notifyDataSetChanged();
        page++;
        if(page>10){
            canLoadMore = false;
            adapter.loadMoreEnd();
        }else{
            adapter.loadMoreComplete();
        }

    }


    private void addNewAddress() {
        startActivity(new Intent(this,EditAddressActivity.class));
        ToastUtil.showToast("添加新地址请求！");
    }

    private class AddressAdapter extends BaseQuickAdapter<AddressItem,BaseViewHolder> {
        public AddressAdapter(int layoutResId, @Nullable List<AddressItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, AddressItem item) {
            if(item.isDefault()){
                helper.setChecked(R.id.cb_default,true);
            }else{
                helper.setChecked(R.id.cb_default,false);
            }
            CheckBox cb = (CheckBox) helper.getView(R.id.cb_default);
            cb.setText("选择");
            helper.addOnClickListener(R.id.cb_default);
            helper.addOnClickListener(R.id.tv_edit);
            helper.addOnClickListener(R.id.tv_delete);
        }
    }
}
